package qc.gateway.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.gateway.entity.*;
import qc.gateway.repository.*;

import java.util.List;

/**
 * API接口权限Service
 *
 * @author QuCheng Tech
 * @since 2024/4/24
 */
@Service
public class ApiAuthorityService {
    private QcUserloginLogsRepository userloginLogsRepository;
    private QcApisRepository qcApisRepository;
    private QcAppKeyRepository qcAppKeyRepository;
    private QcAppsRepository qcAppsRepository;
    private QcDeptsRepository qcDeptsRepository;
    private QcDeptUserRepository qcDeptUserRepository;
    private QcMenuApiRepository qcMenuApiRepository;
    private QcMenusRepository qcMenusRepository;
    private QcRoleApiRepository qcRoleApiRepository;
    private QcRoleDeptRepository qcRoleDeptRepository;
    private QcRoleMenuRepository qcRoleMenuRepository;
    private QcRolesRepository qcRolesRepository;
    private QcRoleUserRepository qcRoleUserRepository;

    @Autowired
    public void setQcUserloginLogsRepository(QcUserloginLogsRepository userloginLogsRepository) {
        this.userloginLogsRepository = userloginLogsRepository;
    }

    @Autowired
    public void setQcApisRepository(QcApisRepository qcApisRepository) {
        this.qcApisRepository = qcApisRepository;
    }

    @Autowired
    public void setQcAppKeyRepository(QcAppKeyRepository qcAppKeyRepository) {
        this.qcAppKeyRepository = qcAppKeyRepository;
    }

    @Autowired
    public void setQcAppsRepository(QcAppsRepository qcAppsRepository) {
        this.qcAppsRepository = qcAppsRepository;
    }

    @Autowired
    public void setQcDeptsRepository(QcDeptsRepository qcDeptsRepository) {
        this.qcDeptsRepository = qcDeptsRepository;
    }

    @Autowired
    public void setQcDeptUserRepository(QcDeptUserRepository qcDeptUserRepository) {
        this.qcDeptUserRepository = qcDeptUserRepository;
    }

    @Autowired
    public void setQcMenuApiRepository(QcMenuApiRepository qcMenuApiRepository) {
        this.qcMenuApiRepository = qcMenuApiRepository;
    }

    @Autowired
    public void setQcMenusRepository(QcMenusRepository qcMenusRepository) {
        this.qcMenusRepository = qcMenusRepository;
    }

    @Autowired
    public void setQcRoleApiRepository(QcRoleApiRepository qcRoleApiRepository) {
        this.qcRoleApiRepository = qcRoleApiRepository;
    }

    @Autowired
    public void setQcRoleDeptRepository(QcRoleDeptRepository qcRoleDeptRepository) {
        this.qcRoleDeptRepository = qcRoleDeptRepository;
    }

    @Autowired
    public void setQcRoleMenuRepository(QcRoleMenuRepository qcRoleMenuRepository) {
        this.qcRoleMenuRepository = qcRoleMenuRepository;
    }

    @Autowired
    public void setQcRolesRepository(QcRolesRepository qcRolesRepository) {
        this.qcRolesRepository = qcRolesRepository;
    }

    @Autowired
    public void setQcRoleUserRepository(QcRoleUserRepository qcRoleUserRepository) {
        this.qcRoleUserRepository = qcRoleUserRepository;
    }

    /**
     * 根据token获取用户登录记录信息
     *
     * @param token 用户登录后的token
     * @return qc.module.platform.entity.QcUserloginLogs
     * @author QuCheng Tech
     * @date 2023/1/11
     */
    public QcUserloginLogs getUserLoginLogByToken(String token) {
        //如输入的token为空，返回null
        if (!StringUtils.isBlank(token)) {
            LambdaQueryWrapper<QcUserloginLogs> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(QcUserloginLogs::getToken, token);

            wrapper.orderByDesc(QcUserloginLogs::getTm);

            QcUserloginLogs logs = userloginLogsRepository.selectOne(wrapper);
            if (logs != null)
                return logs;
        }

        return null;
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcApis>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcApis> getAllQcApis() {
        return qcApisRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcAppKey>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcAppKey> getAllQcAppKeys() {
        return qcAppKeyRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcApps>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcApps> getAllQcApps() {
        return qcAppsRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcDepts>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcDepts> getAllQcDepts() {
        return qcDeptsRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcDeptUser>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcDeptUser> getAllQcDeptUsers() {
        return qcDeptUserRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcMenuApi>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcMenuApi> getAllQcMenuApis() {
        return qcMenuApiRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcMenus>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcMenus> getAllQcMenus() {
        return qcMenusRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcRoleApi>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcRoleApi> getAllQcRoleApis() {
        return qcRoleApiRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcRoleDept>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcRoleDept> getAllQcRoleDepts() {
        return qcRoleDeptRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcRoleMenu>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcRoleMenu> getAllQcRoleMenus() {
        return qcRoleMenuRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcRoles>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcRoles> getAllQcRoles() {
        return qcRolesRepository.selectList(null);
    }

    /***
     * 获取所有
     *
     * @return java.util.List<qc.gateway.entity.QcRoleUser>
     * @author QuCheng Tech
     * @since 2024/4/25
     */
    public List<QcRoleUser> getAllQcRoleUsers() {
        return qcRoleUserRepository.selectList(null);
    }
}
